1216A - Prefixes - CodeForces Solution


strings *800

Please click on ads to support us..

Python Code:

n=int(input())
string=input()
string=list(string)
a=0
b=0
count=0
for i in range(0,n):
    if(string[i]=="a"):
        a+=1
    else:
        b+=1
    if(i%2!=0):
        if(a==b):
            continue
        else:
            if(a>b):
                a-=1
                b+=1
                string[i]="b"
                count+=1
            else:
                b-=1
                a+=1
                string[i]="a"
                count+=1
print(count)
stri="".join(map(str,string))
print(stri)
            
		   	  	     	      	   	 	

C++ Code:

#include<bits/stdc++.h>
#include<vector>
using namespace std;
#define ll long long int


void solve(){
    
    int n;
    cin >> n;
    string s;
    cin >> s;
    int count = 0;
    for(int i = 0; i < int(s.length())-1; i+=2) {
        if(s[i] == s[i+1]) {
            if(s[i] == 'a') s[i] = 'b';
            else s[i] = 'a';
            count++;
        }
    }
    cout << count << endl;
    cout << s;



   
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t = 1;
    //cin >> t;
    while(t--)
        solve();
}


Comments

Submit
0 Comments
More Questions

803B - Distances to Zero
291A - Spyke Talks
1742E - Scuza
1506D - Epic Transformation
1354G - Find a Gift
1426F - Number of Subsequences
1146B - Hate "A"
1718C - Tonya and Burenka-179
834A - The Useless Toy
1407D - Discrete Centrifugal Jumps
1095B - Array Stabilization
291B - Command Line Arguments
1174B - Ehab Is an Odd Person
624B - Making a String
1064C - Oh Those Palindromes
1471A - Strange Partition
1746A - Maxmina
1746B - Rebellion
66C - Petya and File System
1746C - Permutation Operations
1199B - Water Lily
570B - Simple Game
599C - Day at the Beach
862A - Mahmoud and Ehab and the MEX
1525A - Potion-making
1744D - Divisibility by 2n
1744C - Traffic Light
1744A - Number Replacement
1744B - Even-Odd Increments
637B - Chat Order